/*
Do-file name: 6_AKM_estimation.do
Author: Thiago
Date: Nov 7th 2018
This version: April 2021
Comments: This do-file exports dataset for AKM estimation in Stata and produces all figures and tables
related to Concurso data.
*/

use "$temp/judges_estimation.dta", clear

gen Court_Appeal_C_mean = Court_Appeal_C/total_judges_overall
gen court_appeal_IHS = asinh(Court_Appeal_C_mean)

/*Generate Database for Linear Fixed Effects Estimates in R: We estimate the AKM in R, so first export
database to .csv then open in R. The commented command below allows for estimation in R directly from Stata.
*/

preserve
	export delimited Judge Court_name State Judge_idP Court_idP2  YM year month  ///
	Sent_Trial_Merits_J Hearing_J sentence_IHS  hearing_IHS total_courts_overall total_judges_overall ///
	Judge_Court_idP duration_spell_tot period_by_judge_court await_100days_IHS homologacao_IHS ///
	extinction_IHS court_appeal_IHS months_judge_court judges_month ///
	using "$temp/Intermed_FE_R.csv", replace
restore


** Keep one obs per judge and save quintiles of ranks
preserve

	bys Judge_idP: keep if _n == 1
	keep if Concurso_enc != .
	
	gsort Concurso_enc Rank_imp
	by Concurso_enc: gen Rank_concurso = _n 

	*Generating quantiles of exam
	by Concurso_enc: egen total_concurso =  max(Rank_concurso)
	gen quintile = .
	replace quintile  = 5 if (Rank_concurso>0.8*total_concurso)
	replace quintile  = 4 if (Rank_concurso>0.6*total_concurso & Rank_concurso<=0.8*total_concurso)
	replace quintile  = 3 if (Rank_concurso>0.4*total_concurso & Rank_concurso<=0.6*total_concurso)
	replace quintile  = 2 if (Rank_concurso>0.2*total_concurso & Rank_concurso<=0.4*total_concurso)
	replace quintile  = 1 if (Rank_concurso<=0.2*total_concurso)
	replace quintile = . if Rank_concurso == .

	*Alternative Rank - using the raw rank and not only among those merged and in the dataset
	gen Rank_concurso_original = Rank_imp if Rank_concurso !=.
	
	keep Judge_idP Concurso_enc Rank_concurso Rank_concurso_original quintile Final_grade Grade_Titles Final_grade_z Grade_Titles_z Essay_Exam_Civil_z Essay_Exam_Penal_z Objective_Exam_z Oral_Exam_z Written_Exam_z
	
	drop if Concurso_enc == .
	
	save "$temp/judges_level_rank.dta", replace
	
restore

merge m:1 Judge_idP Concurso_enc using "$temp/judges_level_rank.dta"

******************************************************************************
** Table 4: Reduced form regressions: output and admission exam performance **
******************************************************************************

eststo drop *

eststo: reghdfe sentence_IHS ib5.quintile, a(Concurso_enc) cluster(Judge_idP) 
estadd local concurso "Yes"
estadd local Court "No"
estadd local Month "No"
estadd local Court_month "No"
test 1.quintile == 2.quintile
estadd scalar p2 = `r(p)'
test 1.quintile == 3.quintile
estadd scalar p3 = `r(p)'
test 1.quintile == 4.quintile
estadd scalar p4 = `r(p)'

eststo: reghdfe sentence_IHS ib5.quintile, a(Concurso_enc Court_idP2) cluster(Judge_idP) 
estadd local concurso "Yes"
estadd local Court "Yes"
estadd local Month "No"
estadd local Court_month "No"
test 1.quintile == 2.quintile
estadd scalar p2 = `r(p)'
test 1.quintile == 3.quintile
estadd scalar p3 = `r(p)'
test 1.quintile == 4.quintile
estadd scalar p4 = `r(p)'

eststo: reghdfe sentence_IHS ib5.quintile, a(Concurso_enc Court_idP2 YM) cluster(Judge_idP) 
estadd local concurso "Yes"
estadd local Court "Yes"
estadd local Month "Yes"
estadd local Court_month "No"
test 1.quintile == 2.quintile
estadd scalar p2 = `r(p)'
test 1.quintile == 3.quintile
estadd scalar p3 = `r(p)'
test 1.quintile == 4.quintile
estadd scalar p4 = `r(p)'

eststo: reghdfe sentence_IHS ib5.quintile, a(Concurso_enc Court_YM) cluster(Judge_idP) 
estadd local concurso "Yes"
estadd local Court "No"
estadd local Month "No"
estadd local Court_month "Yes"
test 1.quintile == 2.quintile
estadd scalar p2 = `r(p)'
test 1.quintile == 3.quintile
estadd scalar p3 = `r(p)'
test 1.quintile == 4.quintile
estadd scalar p4 = `r(p)'

esttab using "$paper_tables/reduced_regs_quintile.tex", keep(1.quintile 2.quintile 3.quintile 4.quintile) ///
	booktabs f replace stats(N r2 concurso Court Month Court_month p2 p3 p4 , fmt(%9.0gc %9.2fc) ///
	labels(Observations R-Squared "Concurso FE" "Court FE" "Month FE" "Court-by-Month FE" ///
		"$\beta_1 = \beta_2$" "$\beta_1 = \beta_3$" "$\beta_1 = \beta_4$" )) label  nomtitles se ///
	r2 coeflabels(1.quintile_all "Top quintile ($\beta_1$)" 2.quintile_all "4th quintile ($\beta_2$)"  ///
		3.quintile_all "3rd quintile ($\beta_3$)"  4.quintile_all "2nd quintile ($\beta_4$)") substitute(\_ _) ///
	star(* 0.10 ** 0.05 *** 0.01) 

******************************************************************************
***** Table A8: Relationship between admission grades and time in court ******
******************************************************************************

sort Judge_idP Court_idP2 YM

gegen time_court = max(months_judge_court), by(Judge_idP Court_idP2)

eststo drop *

eststo: reghdfe time_court ib5.quintile, a(Concurso_enc) cluster(Judge_idP) 
estadd local concurso "Yes"
estadd local Court "No"
estadd local Month "No"
estadd local Court_month "No"
estadd local nivel  "Judge-Court-Month"

eststo: reghdfe time_court ib5.quintile, a(Concurso_enc Court_idP2) cluster(Judge_idP) 
estadd local concurso "Yes"
estadd local Court "Yes"
estadd local Month "No"
estadd local Court_month "No"
estadd local nivel  "Judge-Court-Month"

eststo: reghdfe time_court ib5.quintile, a(Concurso_enc Court_idP2 YM) cluster(Judge_idP) 
estadd local concurso "Yes"
estadd local Court "Yes"
estadd local Month "Yes"
estadd local Court_month "No"
estadd local nivel  "Judge-Court-Month"

eststo: reghdfe time_court ib5.quintile, a(Concurso_enc Court_YM) cluster(Judge_idP) 
estadd local concurso "Yes"
estadd local Court "No"
estadd local Month "No"
estadd local Court_month "Yes"
estadd local nivel  "Judge-Court-Month"

esttab using "$paper_tables/reduced_regs_time.tex", drop(_cons  5.quintile) booktabs  f replace ///
	stats(N r2 concurso Court Month Court_month nivel, fmt(%9.0gc %9.2fc) ///
	labels(Observations R-Squared "Concurso FE" "Court FE" "Month FE" "Court-by-Month FE" "Observation level" )) label  nomtitles se ///
	r2 star(* 0.10 ** 0.05 *** 0.01) ///
	coeflabels(1.quintile "Top quintile" 2.quintile "4th quintile"  3.quintile "3rd quintile"  ///
		4.quintile "2nd quintile")

******************************************************************************
**** Table A9: Relationship between time in court and case disposition *******
******************************************************************************

sort Judge_idP Court_idP2 YM
gen months_judge_court_sqr = months_judge_court^2

eststo drop *

eststo: reghdfe sentence_IHS months_judge_court months_judge_court_sqr, a(Concurso_enc) cluster(Judge_idP) 
estadd local concurso "Yes"
estadd local Court "No"
estadd local Month "No"
estadd local Court_month "No"
estadd local nivel  "Judge-Court-Month"

eststo: reghdfe sentence_IHS months_judge_court months_judge_court_sqr , a(Concurso_enc Court_idP2) cluster(Judge_idP) 
estadd local concurso "Yes"
estadd local Court "Yes"
estadd local Month "No"
estadd local Court_month "No"
estadd local nivel  "Judge-Court-Month"

eststo: reghdfe sentence_IHS months_judge_court months_judge_court_sqr, a(Concurso_enc Court_idP2 YM) cluster(Judge_idP) 
estadd local concurso "Yes"
estadd local Court "Yes"
estadd local Month "Yes"
estadd local Court_month "No"
estadd local nivel  "Judge-Court-Month"

eststo: reghdfe sentence_IHS months_judge_court months_judge_court_sqr , a(Concurso_enc Court_YM) cluster(Judge_idP) 
estadd local concurso "Yes"
estadd local Court "No"
estadd local Month "No"
estadd local Court_month "Yes"
estadd local nivel  "Judge-Court-Month"

eststo: reghdfe sentence_IHS months_judge_court months_judge_court_sqr if months_judge_court >=3, a(Concurso_enc Court_idP2 YM) cluster(Judge_idP) 
estadd local concurso "Yes"
estadd local Court "Yes"
estadd local Month "Yes"
estadd local Court_month "No"
estadd local nivel  "Judge-Court-Month"

esttab using "$paper_tables/reduced_regs_experience_product.tex", drop(_cons ) booktabs  f replace ///
	stats(N r2 concurso Court Month Court_month nivel, fmt(%9.0gc %9.2fc) ///
	labels(Observations R-Squared "Concurso FE" "Court FE" "Month FE" "Court-by-Month FE" "Observation level" )) label  nomtitles se ///
	r2 star(* 0.10 ** 0.05 *** 0.01) ///
	coeflabels(months_judge_court "Months in court" months_judge_court_sqr "Months in court (squared)")
	
